package com.lightkits.wms.domain.pickreport;

import com.lightkits.common.CommonToolsUtil;
import com.lightkits.framework.common.model.CheckResult;
import com.lightkits.framework.common.model.IResult;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.domain.pickreport.service.PickReportDomainService;
import com.lightkits.wms.dto.pickreport.CancelPickReportRequestType;
import com.lightkits.wms.dto.pickreport.CancelPickReportResponseType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * CancelPickReportExecutor
 *
 * @author zhuangwei.zheng@hand-china.com
 * Created on 2019/5/9 13:57
 */
@Component
public class CancelPickReportExecutor implements ServiceExecutor<CancelPickReportRequestType, CancelPickReportResponseType> {

    private final PickReportDomainService pickReportDomainService;

    @Autowired
    public CancelPickReportExecutor(PickReportDomainService pickReportDomainService) {
        this.pickReportDomainService = pickReportDomainService;
    }

    @Override
    public CancelPickReportResponseType service(CancelPickReportRequestType request) {
        CancelPickReportResponseType response = new CancelPickReportResponseType();
        response.setSuccessful(true);
        try {
            IResult checkParam = pickReportDomainService.checkCancelParam(request);
            if (!checkParam.isSuccessful()) {
                return checkParam.fill(response);
            }

            CheckResult<ExecuteResModel> result = pickReportDomainService
                    .cancelPickReport(request);
            if (!result.isSuccessful()) {
                return result.fill(response);
            }

            pickReportDomainService.cancelTransaction(result.getData());
        } catch (Exception e) {
            CommonToolsUtil.executeException(e, response);
        }
        return response;
    }
}
